package com.jdk.demo.fileChannel.write;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Test1_1 {

	// 验证write是从通道的当前位置开始写入的
	public static void main(String[] args) throws Exception {
		FileOutputStream fosRef = new FileOutputStream("a.txt");
		FileChannel fileChannel = fosRef.getChannel();
		try{
			ByteBuffer buffer = ByteBuffer.wrap("abcde".getBytes());
			System.out.println("A fileChannel.position=" + fileChannel.position());
			System.out.println("write 1 返回值：" + fileChannel.write(buffer));
			System.out.println("B fileChannel.position=" + fileChannel.position());
			fileChannel.position(2);
			buffer.rewind();
			// 然后在当前位置position中再进行写入
			System.out.println("write 2 返回值：" + fileChannel.write(buffer));
			System.out.println("B fileChannel.position=" + fileChannel.position());
		}catch(Exception e){
			e.printStackTrace();
		}
		fileChannel.close();
		fosRef.close();
	}
	
}
